package com.mf.elephant.web.analyze;

import com.mf.elephant.domain.analyzer.mysql.ColumnGenerator;
import com.mf.elephant.domain.analyzer.mysql.MysqlGenerator;
import com.mf.elephant.service.analyze.MysqlGeneratorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * MySQL元数据代码生成Controller
 * User: zhaoming
 * DateTime: 2015-09-08
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/web/mysqlGenerator")
public class MysqlGeneratorController {

    public Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    private MysqlGeneratorService mysqlGeneratorService;
    /**
     * 初始化
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("analyze/mysql-generator-index");
        return modelAndView;
    }

    /**
     * 数据库表信息查询
     */
    @RequestMapping(value = "/selectDatabase", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ColumnGenerator> selectDatabase() {
        List<ColumnGenerator> columnGeneratorList =  mysqlGeneratorService.selectDatabase();
        return columnGeneratorList;
    }

    /**
     * 数据库表信息查询
     */
    @RequestMapping(value = "/selectTableNames", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ColumnGenerator> selectTableNames(ColumnGenerator columnGenerator) {
        List<ColumnGenerator> columnGeneratorList =  mysqlGeneratorService.selectTableNames(columnGenerator);
        return columnGeneratorList;
    }

    /**
     * 字段信息查询
     */
    @RequestMapping(value = "/selectColumnNames", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> selectColumnNames(ColumnGenerator columnGenerator) {
        List<ColumnGenerator> columnGeneratorList =  mysqlGeneratorService.selectColumnNames(columnGenerator);
        Map<String, Object> result = new HashMap<>();
        result.put("root", columnGeneratorList);
        return result;
    }

    /**
     * 生成代码-压缩包下载
     */
    @RequestMapping(value = "/downLoad")
    @ResponseBody
    public ResponseEntity<byte[]> downLoad(MysqlGenerator mysqlGenerator){
        HttpHeaders headers = new HttpHeaders();
        return mysqlGeneratorService.generatorDownLoad(mysqlGenerator, headers);
    }

}